package com.txy.webmanagement.control;

import com.txy.webmanagement.dto.SysUserDTO;
import com.txy.webmanagement.model.SysUser;
import com.txy.webmanagement.service.SysUserService;
import com.txy.webmanagement.utils.Md5Utils;
import com.txy.webmanagement.utils.RequestDataUtils;
import com.txy.webmanagement.utils.StringUtils;
import com.txy.webmanagement.utils.SysParrentUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 登录
 */
@Controller
public class LoginControl {
    @Autowired
    private SysUserService userService;

    /**
     * login页面
     *
     * @return
     */
    @RequestMapping(value = "/")
    public String index() {
        return "login/login";
    }

    /**
     * 用户登录验证
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "login")
    @ResponseBody
    public RequestDataUtils login(SysUserDTO user) {
        RequestDataUtils res = new RequestDataUtils();
        String userName = user.getUserName();
        String passwrd = user.getPassword();
        if (StringUtils.checkStringNotNull(userName) && StringUtils.checkStringNotNull(passwrd)) {
            SysUser useMd = userService.getSysUserByUserName(userName);
            if (useMd != null) {
                String pasSql = useMd.getPassword();
                String md5Psw = Md5Utils.getResult(passwrd);
                if (pasSql.equals(md5Psw)) {
                    Subject sub = SecurityUtils.getSubject();
                    UsernamePasswordToken token = new UsernamePasswordToken(userName,md5Psw);
                    sub.login(token);
                    res.setCode("200");
                    res.setMessage("登陆成功!");
                } else {
                    res.setCode("300");
                    res.setMessage("密码不正确!");
                }
            } else {
                res.setCode("300");
                res.setMessage("用户录信息不存在，请注册!");
            }
        } else {
            res.setCode("300");
            res.setMessage("登录信息不能为空!");
        }
        return res;
    }

    /**
     * 注册页面
     *
     * @return
     */
    @RequestMapping(value = "registerIndex")
    public String registerIndex() {
        return "login/register";
    }

    /**
     * 注册验证
     *
     * @return
     */
    @RequestMapping(value = "register")
    @ResponseBody
    public RequestDataUtils register(SysUserDTO user) {
        return userService.register(user);
    }


    @RequestMapping(value = "pswreminderIndex")
    public String pswreminderIndex() {
        return "login/pswreminder";
    }

    @RequestMapping(value = "pawdReset")
    @ResponseBody
    public RequestDataUtils pawdReset(SysUserDTO dto, HttpSession session) {
        String sessionId = session.getId();
        String s_vfcCode = SysParrentUtils.vfcPasCode(sessionId);
        String sCode = (String) session.getAttribute(s_vfcCode);
        if (StringUtils.checkStringNotNull(sCode)) {
            if (sCode.equals(dto.getVfCode().trim())) {
                RequestDataUtils res = userService.pawdReset(dto);
                return res;
            } else {
                RequestDataUtils res = userService.pawdReset(dto);
                res.setCode(SysParrentUtils.errorCode);
                res.setMessage("验证码输入错误，请重新输入！");
                return res;
            }
        } else {
            RequestDataUtils res = new RequestDataUtils();
            res.setCode(SysParrentUtils.errorCode);
            res.setMessage("信息超时，请重新刷新获取验证码");
            return res;
        }

    }

    @RequestMapping(value = "logout")
    public String logout() {

        return "login/loginout";
    }
}
